package com.bestv.lucene;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.IOException;

/**
 * 索引读取器
 * User: Henry
 * Date: 14-5-6
 */
public class IndexFileReader {
    private IndexReader indexReader;

    public IndexFileReader(String indexDir) throws IOException {
        File file = new File(indexDir);
        Directory dir = FSDirectory.open(file);
        indexReader = DirectoryReader.open(dir);
    }

    public void close() throws IOException {
        indexReader.close();
    }

    public void info() throws IOException {
        System.out.println("版本: " + indexReader.numDocs());
        System.out.println("索引文件数: " + indexReader.numDocs());
        System.out.println("索引文件列表:");
        for (int i=0; i<indexReader.numDocs(); i++) {
            System.out.println(indexReader.document(i));
        }
    }
}
